<?php

namespace App\Console\Commands\User;

use App\Kuafu\User\Enum\RoleCodeEnum;
use App\Kuafu\User\Model\User;
use App\Kuafu\User\Model\UserRoleMapping;
use Illuminate\Console\Command;

class RefreshMerchantUserDimissionList extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:refresh-merchant-user-dimission-list {type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '刷新商家用户离职列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $type = $this->argument('type');

        switch ($type) {
            case "time":
                // 刷新老员工的入职时间
                // 获得所有人的入职时间

                $entryTimeList = [];
                $leaveTimeList = [];
                User::query()
                    ->select(['id', 'createdAt', 'updatedAt'])
                    ->chunk(1000, function ($users) use (&$entryTimeList, &$leaveTimeList) {
                        foreach ($users as $user) {
                            $entryTimeList[$user->id] = $user->createdAt;
                            $leaveTimeList[$user->id] = $user->updatedAt;
                        }
                    });

                // 处理入职时间
                foreach ($entryTimeList as $userId => $entryTime) {
                    echo '.';
                    UserRoleMapping::query()
                        ->where('userId', $userId)
                        ->withoutGlobalScope('isDel')
                        ->whereIn('type', [RoleCodeEnum::ROLE_POS_TYPE, RoleCodeEnum::ROLE_BUSINESS_TYPE])
                        ->update([
                            'entryTime' => $entryTime
                        ]);
                }

                // 处理离职时间
                foreach ($leaveTimeList as $userId => $leaveTime) {
                    echo '.';
                    UserRoleMapping::query()
                        ->where('userId', $userId)
                        ->withoutGlobalScope('isDel')
                        ->where('isDeleted', 1)
                        ->whereIn('type', [RoleCodeEnum::ROLE_POS_TYPE, RoleCodeEnum::ROLE_BUSINESS_TYPE])
                        ->update([
                            'leaveTime' => $leaveTime
                        ]);
                }
                break;
            case "bimission":
                // 获得RMS离职员工列表 已离职的员工处理
                UserRoleMapping::query()
                    ->whereRaw("userId in (select id from kf_users where status = 1)")
                    ->whereIn('type', [RoleCodeEnum::ROLE_POS_TYPE, RoleCodeEnum::ROLE_BUSINESS_TYPE])
                    ->update([
                        'isDeleted' => 1
                    ]);
                break;
            default:
        }
    }
}
